import requests
from bs4 import BeautifulSoup
import pandas as pd

# 目标网址
url = "https://lz.esf.fang.com"

# 发送请求到网站
response = requests.get(url)
response.encoding = 'utf-8'
soup = BeautifulSoup(response.text, 'html.parser')

# 查找房源列表的元素
listings = soup.find_all('dl', class_='clearfix')

# 定义列表存储提取的数据
types = []
areas = []
orientations = []
floors = []
total_prices = []
unit_prices = []

# 提取所需数据
for listing in listings:
    # 提取户型
    type_tag = listing.find('p', class_='tel_shop')
    if type_tag:
        type_text = type_tag.get_text(strip=True).split('|')[0]
    else:
        type_text = None
    types.append(type_text)

    # 提取建筑面积
    if type_tag:
        area_text = type_tag.get_text(strip=True).split('|')[1]
    else:
        area_text = None
    areas.append(area_text)

    # 提取朝向
    if type_tag and len(type_tag.get_text(strip=True).split('|')) > 3:
        orientation_text = type_tag.get_text(strip=True).split('|')[3]
    else:
        orientation_text = None
    orientations.append(orientation_text)

    # 提取楼层
    if type_tag and len(type_tag.get_text(strip=True).split('|')) > 2:
        floor_text = type_tag.get_text(strip=True).split('|')[2]
    else:
        floor_text = None
    floors.append(floor_text)

    # 提取总价
    total_price_tag = listing.find('dd', class_='price_right')
    if total_price_tag:
        total_price_span = total_price_tag.find('span', class_='red')
        if total_price_span:
            total_price_text = total_price_span.find('b').get_text(strip=True) + '万'
        else:
            total_price_text = None
    else:
        total_price_text = None
    total_prices.append(total_price_text)

    # 提取单价
    if total_price_tag:
        unit_price_text = total_price_tag.get_text(strip=True).split('元')[0] + '元/m²'
    else:
        unit_price_text = None
    unit_prices.append(unit_price_text)

# 创建DataFrame
df = pd.DataFrame({
    '户型': types,
    '建筑面积': areas,
    '朝向': orientations,
    '楼层': floors,
    '总价': total_prices,
    '单价': unit_prices
})

# 保存到CSV文件
df.to_csv('兰州二手房信息.csv', index=False)

print("数据已保存到兰州二手房信息.csv")